2. Why use Spring with Struts ?
It is a lightweight dependency injection and
aspect-oriented container and framework.
Promote Loose Coupling through Dependancy
Injection.
Most popular technology for managing creation
of objects in a Java application.
Has its own MVC application framework.
KrazyKoder.com
3. Adding Spring to Struts2 Contd…..
Add the Spring plug-in to application.
Add
org.springframework.web.context.ContextLoaderListener
to web.xml in listener tag.
ApplicationContext.xml file will be used for doing the
Spring bean configuration.
Spring Provides the autowiring mechanism through
which it automatically inject dependencies without
explicitly declaring the wiring in applicationContext.xml
Autowiring can be done by name, type, constructor or
auto. KrazyKoder.com
4. Adding Spring to Struts2 …..Contd.
All objects which spring manages are called “Spring Beans” and all spring
beans are created as Singleton.
Add bean to applicationContext.xml file and change scope of bean to
Prototype.
ID in first bean to identify the bean, much like a reference is used to identify
an object in Java code.
Property tag looks for setter method with the same name as the property
tag’s name attribute.
Value to inject into this setter is specified with the ref attribute
Ex : <bean id="portfolioService"
class="manning.chapterNine.utils.PortfolioServiceJPAImpl"/>
<bean id="springManagedLoginAction” class="manning.chapterNine.Login“
scope="prototype">
<property name="portfolioService" ref="portfolioService"/>
KrazyKoder.com
</bean>
5. Adding JPA with Struts 2?
This technology, wrapped around Hibernate.
Coding to the JPA works a lot like coding to native
Hibernate.
Interface-based architecture gives you the ability to
switch out the vendor supplying the underlying
implementation.
Shows how to use the well-respected Spring support for
JPA to make management of the JPA all that much
easier.
Main entry point into the JPA is EntityManager. This
object manages all persistent entities. KrazyKoder.com
6. Adding JPA with Struts 2?
Declare a bean postprocessor that checks all the beans
managed by Spring for persistence-related annotations
EntityManagerFactory managed by Spring encapsulates
most of the details of persistence unit.
Main entry point into configuring persistence unit is the
persistence.xml file.
/WEB-INF/classes/METAINF/persistence.xml
Through JPA we can use Java annotations to map java
classes to database.
KrazyKoder.com
7. Done!
Click the icon at the bottom to proceed!
KrazyKoder.com